package com.ly.sms.controller.login;

import com.ly.sms.model.ApiResponse;
import com.ly.sms.model.LoginRequest;
import com.ly.sms.model.LoginRsp;
import com.ly.sms.model.RspEnum;
import com.ly.sms.model.User;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author ly
 * @Date 2022-03-26 12:11
 */
@RestController
@RequestMapping(path = {"/auth/user"})
public class LoginController {

  public static volatile Map<String, User> users = new ConcurrentHashMap<>(32);

  public static volatile Map<String, String> token = new ConcurrentHashMap<>(32);

  @RequestMapping(path = {"/register"})
  @ResponseBody
  public ApiResponse register(@RequestBody LoginRequest request) {

    // 参数校验
    if (request.getUserName() == null || "".equals(request.getUserName().trim()) ||
        request.getPassword() == null || "".equals(request.getPassword().trim())) {
      return new ApiResponse(RspEnum.PARAM_ERROR.getCode(), RspEnum.PARAM_ERROR.getMsg());
    }

    // 重复注册用户
    if (users.get(request.getUserName()) != null) {
      return new ApiResponse(RspEnum.REPEAT_REGIST.getCode(), RspEnum.REPEAT_REGIST.getMsg());
    }

    User user = new User();
    user.setUserName(request.getUserName());
    user.setPassword(request.getPassword());

    users.put(user.getUserName(), user);
    return new ApiResponse(RspEnum.SUCCESS.getCode(), RspEnum.SUCCESS.getMsg());
  }


  @RequestMapping(path = {"/login"})
  @ResponseBody
  public ApiResponse login(@RequestBody LoginRequest request) {

    // 参数校验
    if (request.getUserName() == null || "".equals(request.getUserName().trim()) ||
        request.getPassword() == null || "".equals(request.getPassword().trim())) {
      return new ApiResponse(RspEnum.PARAM_ERROR.getCode(), RspEnum.PARAM_ERROR.getMsg());
    }

    // 未注册
    User user = users.get(request.getUserName());
    if (user == null) {
      return new ApiResponse(RspEnum.NOT_REGIST.getCode(), RspEnum.NOT_REGIST.getMsg());
    }

    // 密码错误
    if (!user.getPassword().equals(request.getPassword())) {
      return new ApiResponse(RspEnum.PASSWORD_ERROR.getCode(), RspEnum.PASSWORD_ERROR.getMsg());
    }

    token.put(user.getUserName(), UUID.randomUUID().toString());
    return new LoginRsp(RspEnum.SUCCESS.getCode(), RspEnum.SUCCESS.getMsg(), token.get(user.getUserName()));
  }

  @RequestMapping(path = {"/logout"})
  @ResponseBody
  public ApiResponse logout(@RequestBody LoginRequest request) {

    // 参数校验
    if (request.getUserName() == null || "".equals(request.getUserName().trim()) ||
        request.getSessionId() == null || "".equals(request.getSessionId().trim())) {
      return new ApiResponse(RspEnum.PARAM_ERROR.getCode(), RspEnum.PARAM_ERROR.getMsg());
    }

    // 未登陆
    String tk = token.get(request.getUserName());
    if (tk == null) {
      return new ApiResponse(RspEnum.NOT_LOGIN.getCode(), RspEnum.NOT_LOGIN.getMsg());
    }

    if (!tk.equals(request.getSessionId())) {
      return new ApiResponse(RspEnum.SESSION_ERROR.getCode(), RspEnum.SESSION_ERROR.getMsg());
    }

    token.put(request.getUserName(), null);
    return new LoginRsp(RspEnum.SUCCESS.getCode(), RspEnum.SUCCESS.getMsg());
  }

}
